package com.example.news0515.fragments.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.news0515.Bean.Title;
import com.example.news0515.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyRecylerShouYeTitleAdapter extends RecyclerView.Adapter<MyRecylerShouYeTitleAdapter.ViewHolder> {
    private List<Title> titleList;
    private Context context;
    private int layoutPosition;
    private OnItemClickListener onItemClickListener = null;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    public MyRecylerShouYeTitleAdapter(List<Title> arrList, Context context) {
        this.titleList = arrList;
        this.context = context;
    }
    @NonNull
    @Override
    public MyRecylerShouYeTitleAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.shouye_item, parent,false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyRecylerShouYeTitleAdapter.ViewHolder holder, int position) {
        holder.bt.setText(titleList.get(position).getTitlename());
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    layoutPosition = holder.getLayoutPosition();
                    notifyDataSetChanged();
                    onItemClickListener.onItemClick(holder.itemView, (int) holder.itemView.getTag());
                }
            }
        });
        if (position == layoutPosition) {
            holder.bt.setTextColor(Color.parseColor("#ff0000"));
        } else {
            holder.bt.setTextColor(Color.parseColor("#000000"));
        }
    }
    @Override
    public int getItemCount() {
        return titleList != null ? titleList.size():0;
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView bt;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            bt = itemView.findViewById(R.id.shouye_item_tv);
        }
    }
    public void setBg(int pos){
        this.layoutPosition = pos;
        notifyDataSetChanged();
    }
}
